<template>
	<view>
		<view>
			<uni-segmented-control :current="current" :values="items" style-type="text" @clickItem="onClickItem" />
			<view v-if="current == 0">
				<uni-card style="padding-bottom: 20rpx;" v-for="item in pendingApproList" :key="item.id"
					:title="item.applyNo" :extra="item.className">
					<view style="display: flex; padding: 40rpx 20rpx;">
						<image style="width:200rpx; height: 250rpx;"
							:src="item.studentImg !=''? item.studentImg:'https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png'"
							@click="clickImg(item.studentImg)" mode=""></image>
						<view class="" style="font-size: 24rpx; margin-left: 30rpx; width: 200px;">
							<view>学生姓名：{{item.studentName}}</view>
							<view>违纪类型详情：{{item.violationTypeDetail}}</view>
							<view>创建时间：{{item.createTime}}</view>
							<view style="display: flex;">申请单状态：
								<view v-if="item.applyStatus == 0"><text>申请被驳回</text></view>
								<view v-if="item.applyStatus == 1" style="color: #fe0000;"><text>处理中</text></view>
								<view v-if="item.applyStatus == 2"><text>申请被撤回</text></view>
								<view v-if="item.applyStatus == 10"><text style="color: green;">审批通过</text></view>
							</view>
						</view>
					</view>
					<view style="padding: 20rpx 0;">
						<view class="button" @click="approvalHandle(item)">处 理</view>
					</view>
				</uni-card>

				<view class="" style="position: absolute; left: 10%; top: 20%;" v-if="pendingApproList.length <= 0">
					<image src="https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/wujilu.png" mode=""></image>
				</view>
			</view>
			<view v-if="current == 1">
				<uni-card style="padding-bottom: 20rpx;" v-for="(item,index) in approvaledList" :key="index"
					:title="item.businessData.applyNo" :extra="item.businessData.className" @click="toDetail(item)">
					<view style="display: flex; padding: 40rpx 20rpx 0 20rpx; ">
						<view class="">
							<image style="width:200rpx; height: 200rpx;" :src="item.businessData.studentImg" mode="">
							</image>
						</view>
						<view class="" style="font-size: 24rpx; margin-left: 30rpx; width: 200px;">
							<view>学生姓名：{{item.businessData.studentName}}</view>
							<view>班级：{{item.businessData.className}}</view>
							<view>违纪类型详情：{{item.businessData.violationTypeDetail}}</view>
							<view>创建时间：{{item.createTime}}</view>
						</view>
					</view>

				</uni-card>

				<view class="" style="position: absolute; left: 10%; top: 20%;" v-if="approvaledList.length <= 0">
					<image src="https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/wujilu.png" mode=""></image>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import taskNotReturned from "@/api/modules/taskNotReturned.js"
	import teacherClass from "@/api/modules/teachClass.js"
	import violation from "@/api/modules/violation.js"
	import { parseTime } from "@/utils/date.js"
	export default {
		data() {
			return {
				topName: "待处理违纪",
				userName: "",
				queryParams: {
					pageSize: 10,
					pageNum: 1,
				},
				// 分段器
				items: ['待审批列表', '已完成审批列表', ],
				current: 0,
				// 待审批列表
				pendingApproList: [],
				tempPendingAppros: [],
				// 表单数据
				formData: {
					sdId: "",
					studentId: "",
					notReturnedDate: "",
					studentImg: "",
					teacherName: "",
					className: "",
					reason: "",
					flag: "",
					createBy: "",
					taskId: "",
					applyID: "",
					applyTitle: "",
					businessKey: "",
					processInstanceId: "",
				},
				currNode: "",
				dorNoList: [],
				studentList: [],
				flagList: [{
					text: "同意",
					value: 1
				}, {
					text: "驳回",
					value: 0
				}],
				flagListSecond: [{
					text: "同意",
					value: 1
				}],
				approvaledList: [],
				tempApprovaledList: [],
				historyQueryParam: {
					pageSize: 5,
					pageNum: 1,
				},
			}
		},
		onReachBottom() {
			if(this.current == 0) {
				// 加载待审批列表
				this.queryParams.type = "2"
				this.queryParams.createBy = this.userName
				this.queryParams.pageNum += 1
				// this.queryParams.pageSize += 10
				violation.getPageList(this.queryParams).then(res => {
					this.tempPendingAppros = res.data.list
					this.tempPendingAppros.forEach(item => {
						if (item.studentImg == null) {
							item.studentImg = "https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png"
						}
						item.createTime = parseTime(item.createTime)
					})
					this.pendingApproList = [...this.pendingApproList, ...this.tempPendingAppros]
				})
				this.tempPendingAppros = []
				this.$forceUpdate()
			}
			
			if(this.current == 1) {
				this.historyQueryParam.createBy = this.userName
				this.historyQueryParam.pageNum += 1
				this.historyQueryParam.applyID = "sdViolationFlow"
				// 加载已审批列表
				violation.findHistoryApproval(this.historyQueryParam).then(res => {
					this.tempApprovaledList = res.data.list
					this.tempApprovaledList.forEach(item => {
						if (item.businessData.studentImg == null) {
							item.businessData.studentImg = "https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png"
						}
						item.createTime = parseTime(item.createTime)
					})
					this.approvaledList = [...this.approvaledList, ...this.tempApprovaledList]
				})
				this.tempApprovaledList = []
				this.$forceUpdate()
			}
		},
		methods: {
			clickImg(img) {
				uni.previewImage({
					urls: [img], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: '', // 当前显示图片的http链接，默认是第一个
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				})
			},
			toDetail(item) {
				console.log(item)
				uni.navigateTo({
					url: `/pages_main/managementTeacher/violationDetail/violationDetail?item=${JSON.stringify(item)}`
				})
			},
			// 分段器
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			// 查询待审批列表
			getPageList() {
				violation.getPageList({
					pageNum: 1,
					pageSize: 10,
					createBy: this.userName,
					type: "2"
				}).then(res => {
					console.log(res)
					this.pendingApproList = res.data.list
					this.pendingApproList.forEach(item => {
						if (item.studentImg == null) {
							item.studentImg = "https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png"
						}
						item.createTime = parseTime(item.createTime)
					})
				})
			},
			// 审批
			approvalHandle(item) {
				item = JSON.stringify(item)
				uni.navigateTo({
					url: `/pages_main/classTeacher/violationsDisciplineBzr/violationsDisciplineBzr?item=${item}`
				})
			},
			// 表单清空
			reset() {
				this.formData = {
					sdId: "",
					studentId: "",
					notReturnedDate: "",
					studentImg: "",
					teacherName: "",
					className: "",
					reason: "",
					flag: "",
					createBy: "",
					taskId: "",
					applyID: "",
					applyTitle: "",
					businessKey: "",
					processInstanceId: "",
				}
			},
			// 提交
			submit() {
				this.formData.createBy = this.userName
				console.log(this.formData)
				violation.processWorkFlow({
					applyDesc: this.formData.applyDesc,
					applyID: this.formData.applyID,
					applyTitle: this.formData.applyTitle,
					businessKey: this.formData.businessKey,
					currNode: this.formData.currNode,
					flag: this.formData.flag,
					processInstanceId: this.formData.processInstanceId,
					taskId: this.formData.taskId,
					variables: {},
					createBy: this.userName
				}).then(res => {
					this.$refs.popup.close()
					this.getPageList()
					this.reset()
				})
			},
			// 查询已审批列表
			getApprovedList() {
				violation.findHistoryApproval({
					createBy: this.userName,
					pageNum: 1,
					pageSize: 5,
					applyID: "sdViolationFlow",

				}).then(res => {
					// console.log(res)
					this.approvaledList = res.data.list
					console.log(this.approvaledList)
					this.approvaledList.forEach(item => {
						console.log(item.businessData)
						if (item.businessData.studentImg == null) {
							item.businessData.studentImg = "https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png"
						}
						item.createTime = parseTime(item.createTime)
					})
				})
			},
			cancle() {
				this.$refs.popup.close()
			}
		},
		onShow() {
			console.log("pendViolationCt onload")
			this.userName = uni.getStorageSync("userName")
			this.getPageList()
			this.getApprovedList()
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.indexContent {
		height: 100vh;
	}

	/deep/.uni-popup .uni-popup__wrapper[data-v-7c43d41b] {
		width: 80%;
		/* padding: 40rpx 20rpx 40rpx 40rpx; */
	}

	.button {
		width: 50%;
		background-color: #22ad38;
		color: #fff;
		padding: 20rpx;
		border-radius: 10rpx;
		text-align: center;
		font-size: 36rpx;
		margin: 0 auto;
	}
</style>